import { observable, action, autorun,toJS } from 'mobx';
import {WeaTableNew,WeaForm} from 'comsMobx'
const {TableStore} = WeaTableNew;
import { ListStore } from './listStore';
import {Modal} from 'antd';
const confirm = Modal.confirm;
import {WeaTools,WeaLocaleProvider} from "ecCom"
import * as  Apis from '../apis/project';
import {ShareStore} from './shareStore'
import {ExchangeStore } from "./exchangeStore"
import {ProjectInfoStore } from './projectInfoStore';
const getLabel = WeaLocaleProvider.getLabel;

class QueryPrjStore  extends ListStore{	 
	@observable rightMenu =[];
    conditiontype = {conditiontype:"search"};
    @observable locationparams = "";//门户查询条件：我管理的、我参与的、我查看的项目
    @observable showLeft = true;
	 /** 左侧树store */
	 @observable leftTree = [];
	 @observable leftTreeCount = {};
	 @observable leftTreeCountType = [];
     @observable selectedTreeKey = "";
     @observable shareStore = new ShareStore();
     @observable exchangeStore = new ExchangeStore();
     @observable projectInfoStore = new ProjectInfoStore();

    @action
    initDatas=(params={},needReset=false)=>{
        this.loading = true;
        this.locationparams = params;
        const newParams = {...this.conditiontype,...params};
        Apis.getPrjCondition(newParams).then(data=>{
            this.loading = false;
            this.condition = data.condition;
            if(needReset){
                this.resetForm();
            }
            if(this.form && !this.form.isFormInit){
                this.form.initFormFields(data.condition);
                if(this.form.has("name")){
                    this.form.updateFields({name:{value:params.name}},false);
                }else{
                    this.form.add('',{key:"name"});
                    this.form.updateFields({name:{value:params.name}},false);
                }
            }
       })
    }
    
    initTreeDatas=(params={})=>{
        const searchParamsAd = this.form.getFormParams();
        let newParams = { ...searchParamsAd,...this.locationparams };
        if(this.form && this.form.isFormInit){
            newParams = {...this.locationparams,...searchParamsAd };
        }
        Apis.getPrjTypeTreePageList({treetype:'prjsearch',...params,...newParams}).then(data=>{
			this.leftTree =data.treedata;
			this.leftTreeCount = data.treecount;
			this.leftTreeCountType = data.treecountcfg;
            this.selectedTreeKey = data.selectedKeys;
            this.doSearch({prjtype:data.selectedKeys});
		})
     }

    doSearch=(params={})=>{
		this.loading = true;
         //获取表单的参数值
        if(params.prjtype){
            this.appendFormFields({prjtype:{value:params.prjtype}});
        }
        const searchParamsAd = this.form.getFormParams();
        if(searchParamsAd.prjtype){
            this.selectedTreeKey = searchParamsAd.prjtype;
        }
        let newParams = { ...searchParamsAd,...this.locationparams };
        if(this.form && this.form.isFormInit){
            newParams = {...this.locationparams,...searchParamsAd };
        }
        Apis.getQueryPrjList(newParams).then(data=>{
			this.tableStore.getDatas(data.sessionkey,  params.current || 1);
			this.topTabCount = data.tabnum;
			this.dataKey = data.sessionkey;
			this.rightMenu = data.rightMenus;
            this.loading = false;
        })
    }
    reLoad=()=>{
		this.doSearch();
	}
	onShowColumn=()=>{
		this.tableStore.setColSetVisible(true);
		this.tableStore.tableColSet(true)
    }
    setSelectedTreeKey=(key) =>{
        this.selectedTreeKey = key;
    }
    @action
	setLeftShow=(bool) =>{
        this.showLeft = bool;
    }
    
	clearStatus=()=>{
        this.showSearchAd = false;
        this.selectedTreeKey = "";
        this.resetForm();
    }
    //项目执行操作
    @action
    doPlanOpt=(params={})=>{
        Apis.doPlanOpt(params).then(data=>{
			this.doSearch();
		});
	}

    //项目删除
    @action
    delPrjInfo=(params={})=>{
        let _this = this;
        const searchParamsAd = this.form.getFormParams();
        let newParams = { ...searchParamsAd,...this.locationparams };
        if(this.form && this.form.isFormInit){
            newParams = {...this.locationparams,...searchParamsAd };
        }
        Modal.confirm({
            title: getLabel(15172,"系统提示"),
            content: getLabel(383860,"您确认要删除项目吗？"),
            onOk() {
                Apis.delPrjInfo(params).then(data=>{
                    Apis.getPrjTypeTreePageList({treetype:'prjsearch',newParams}).then(data=>{
                        _this.leftTree =data.treedata;
                        _this.leftTreeCount = data.treecount;
                        _this.leftTreeCountType = data.treecountcfg;
                        _this.doSearch({prjtype:_this.selectedTreeKey});
                    })
                });
            },
            onCancel() { },
        })
    }

    //导出项目
    @action
    doPrjExcelExp=(params={})=>{
        this.loading = true;
        const searchParamsAd = this.form.getFormParams();
        let newParams = { ...searchParamsAd,...this.locationparams };
        if(this.form && this.form.isFormInit){
            newParams = {...this.locationparams,...searchParamsAd };
        }
        
        WeaTools.callApi('/api/proj/pcproject/doPrjExcelExp','GET',newParams).then(data => {
            window.location="/weaver/weaver.file.ExcelOut";
            this.loading = false;
       }).catch(err=>message.error(err))
    }
}

const queryPrjStore = new QueryPrjStore();
export default queryPrjStore;